package app.client.collect.cartoon.web.bilibili;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import app.client.collect.cartoon.BaseCollectAnalyse;
import app.client.collect.cartoon.entity.CartoonCollectEntity;
import cn.lily.com.file.io.FileInputManager;
import cn.lily.com.lang.Basic;

/**
 * B站分析
 * @author JiaJ
 * @date 2018年1月12日
 *
 */
public class BilibiliAnalyse extends BaseCollectAnalyse {

	public void check(String filePath){
		
		File file = new File(filePath);		
		this.check(file);
			
	}
	
	public void check(File file){
		
		FileInputManager fileManager = new FileInputManager();
		
		fileManager.loadFile(file);
		
		//System.out.println(file.getAbsolutePath());
		
		try {
			
			String line1 = fileManager.readLine();
			
			while(line1 != null){
				
				String line2 = fileManager.readLine();
				String line3 = fileManager.readLine();
				
				if(line2.indexOf("第")>=0){
					
					if(line2.endsWith("季")){
						
					}
					
					else if(line2.endsWith("期")){
						
					}
					
					else if(line2.endsWith("幕")){
						
					}
					
					else if(line2.endsWith("部")){
						
					}
					
					else if(line2.endsWith("季（国语）")){
						
					}
					
					else if(line2.endsWith("季【国语】")){
						
					}
					
					else if(line2.endsWith("季【日语】")){
						
					}
					
					else if(line2.endsWith("季）")){
						
					}
					
					else if(line2.endsWith("季 （僅限港澳台地區）")){
						
					}
					
					else if(line2.endsWith("季（僅台灣地區）")){
						
					}
					
					else if(line2.endsWith("季（台港澳）")){
						
					}
					
					else if(line2.endsWith("季（僅限台灣地區）")){
						
					}
										
					else{
						//System.out.println(line2);
					}

				}
				
				else if(line2.endsWith("SP")){
					//System.out.println(line2);
				}
				
				else if(line2.endsWith("OVA")){
					System.out.println(line2);
				}
				
				else {
					
				}
				
				if(line3 == null){					
					System.out.println("line3 == null:"+line1);
					System.out.println("line3 == null:"+line2);
				}
				
				if(!line3.startsWith("共") && !line3.startsWith("全") && !line3.startsWith("更新至第") && !line3.startsWith("即将开播")){
					
					
					if(line3.endsWith("话")){
						
					}else{
						System.out.println(line3);
					}
					
				}
				
				line1 = fileManager.readLine();
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void analyseFile(String filePath){
		File file = new File(filePath);		
		this.analyseFile(file);
	}
	
	public void analyseFile(File file){
		
		List<CartoonCollectEntity> entitys = new ArrayList<CartoonCollectEntity>();
		
		FileInputManager fileManager = new FileInputManager();
		
		fileManager.loadFile(file);
		
		System.out.println(file.getAbsolutePath());
		
		String fileName = file.getName();
		
		int pointIdx =  fileName.indexOf(".");
		
		String tmpName = fileName.substring(0,pointIdx);
		
		try {
					
			String line1 = fileManager.readLine();
			
			while(line1 != null){
				
				CartoonCollectEntity entity = new CartoonCollectEntity();
				
				String line2 = fileManager.readLine();
				String line3 = fileManager.readLine();
				
				if(line2 == null){
					System.out.println(line1);
				}
				
				if(line2.indexOf("第")>=0){
					
					line2 = line2.trim();
					
					int startIdx = line2.indexOf("第");
					
					if(line2.endsWith("季")){						
						entity.setCartoonName(line2.substring(0,startIdx).trim());
						entity.setSeason(line2.substring(startIdx+1,line2.length()-1).trim());						
					}
					
					else if(line2.endsWith("期")){
						entity.setCartoonName(line2.substring(0,startIdx).trim());
						entity.setSeason(line2.substring(startIdx+1,line2.length()-1).trim());	
					}
					
					else if(line2.endsWith("幕")){
						entity.setCartoonName(line2.substring(0,startIdx).trim());
						entity.setSeason(line2.substring(startIdx+1,line2.length()-1).trim());	
					}
					
					else if(line2.endsWith("部")){
						entity.setCartoonName(line2.substring(0,startIdx).trim());
						entity.setSeason(line2.substring(startIdx+1,line2.length()-1).trim());	
					}
					
					else if(line2.endsWith("季（国语）")){
						entity.setCartoonName(line2.substring(0,startIdx).trim());
						entity.setSeason(line2.substring(startIdx+1,line2.length()-5).trim());	
					}
					
					else if(line2.endsWith("季【国语】")){
						entity.setCartoonName(line2.substring(0,startIdx).trim());
						entity.setSeason(line2.substring(startIdx+1,line2.length()-5).trim());	
					}
					
					else if(line2.endsWith("季【日语】")){
						entity.setCartoonName(line2.substring(0,startIdx).trim());
						entity.setSeason(line2.substring(startIdx+1,line2.length()-5).trim());	
					}
					
					else if(line2.endsWith("季）")){
						entity.setCartoonName(line2.substring(0,startIdx).trim());
						entity.setSeason(line2.substring(startIdx+1,line2.length()-2).trim());	
					}
					
					else if(line2.endsWith("季 （僅限港澳台地區）")){
						entity.setCartoonName(line2.substring(0,startIdx).trim());
						entity.setSeason(line2.substring(startIdx+1,line2.length()-10).trim());	
					}
					
					else if(line2.endsWith("季（僅台灣地區）")){
						entity.setCartoonName(line2.substring(0,startIdx).trim());
						entity.setSeason(line2.substring(startIdx+1,line2.length()-8).trim());	
					}
					
					else if(line2.endsWith("季（台港澳）")){
						entity.setCartoonName(line2.substring(0,startIdx).trim());
						entity.setSeason(line2.substring(startIdx+1,line2.length()-6).trim());	
					}
					
					else if(line2.endsWith("季（僅限台灣地區）")){
						entity.setCartoonName(line2.substring(0,startIdx).trim());
						entity.setSeason(line2.substring(startIdx+1,line2.length()-9).trim());	
					}
										
					else{
						entity.setCartoonName(line2);
						entity.setSeason("1");
					}

				}
				
				else if(line2.endsWith("SP")){
					entity.setCartoonName(line2.substring(0,line2.length()-2).trim());
					entity.setSeason("SP");
				}
				
				else if(line2.endsWith("OVA")){
					entity.setCartoonName(line2.substring(0,line2.length()-3).trim());
					entity.setSeason("OVA");
				}
				
				else {
					entity.setCartoonName(line2);
					entity.setSeason("1");
				}
				
				if(line3 == null){					
					System.out.println("line3 == null:"+line1);
					System.out.println("line3 == null:"+line2);
				}
				
				
				if(line3.startsWith("共")){
					if(line3.endsWith("话")){						
						entity.setCartoonNum(Basic.getInt(line3.substring(1,line3.length()-1)));
						entity.setStatus("0");
					}
				}
				
				else if(line3.startsWith("全")){
					if(line3.endsWith("话")){						
						entity.setCartoonNum(Basic.getInt(line3.substring(1,line3.length()-1)));
						entity.setStatus("0");						
					}
				}
				
				else if(line3.startsWith("更新至第")){
					entity.setCartoonNum(Basic.getInt(line3.substring(4,line3.length()-1)));
					entity.setStatus("1");
				}
				
				else if(line3.startsWith("即将开播")){
					line1 = fileManager.readLine();
					continue;
				}
				
				else {
					entity.setCartoonNum(0);
					entity.setStatus("0");
				}
				
				//年份
				if(tmpName.startsWith("#")){
					entity.setYear("#");
				}else{
					entity.setYear(tmpName);
				}
				
				entitys.add(entity);
				
				line1 = fileManager.readLine();
							
			}
			

			
			this.export(tmpName, entitys);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void analyse(String folderPath) throws Exception{
		
		this.createWriter();
		
		File file = new File(folderPath);
		
		File[] files = file.listFiles();
		
		for(File active:files){
			this.analyseFile(active);
		}
		
		this.save();
		
		this.free();
	
	}
	
	public static void main(String[] args) throws Exception {
		
		BilibiliAnalyse bilibili = new BilibiliAnalyse();
		
		bilibili.analyse("E:/Analyse/Bilibili/UTF8");
		
	}
	
}
